package com.hn658.base.message.managermq;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.xml.ResourceEntityResolver;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.jms.listener.DefaultMessageListenerContainer;
import org.springframework.stereotype.Component;

import com.hn658.base.message.managermq.container.MqContainerResource;
import com.hn658.base.message.managermq.container.MqListenerContainer;
import com.hn658.base.message.managermq.contants.MQManagerContants;
import com.hn658.framework.logging.Logger;
import com.hn658.framework.logging.LoggerFactory;
import com.hn658.framework.logging.enums.LogCategory;

/**
 * MQ队列监听管理器
 *
 * @author ztjie
 */
@Component
public class MessageListenerManager implements ApplicationContextAware {

	/**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(MessageListenerManager.class);
    
    private  ConfigurableApplicationContext context;
    
    private XmlBeanDefinitionReader beanDefinitionReader;
    
    public MessageListenerManager(){
    	
    }
    public void init(){
    	beanDefinitionReader = new XmlBeanDefinitionReader((BeanDefinitionRegistry)  
    			context.getBeanFactory());    
        beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(context));   
    }
    /**
     * 
     * <p>通过应用ID及MQ队列序号，获得MQ队列监听</p> 
     * @author ztjie
     * @date 2015-11-19 下午3:08:06
     * @param appId 应用ID
     * @param index 应用配置的MQ队列序号
     * @return
     * @see
     */
    public MqListenerContainer get(String appId, Integer index){
    	String key = appId+"_"+index;
    	String beanName=MQManagerContants.CONTAINER+"_"+key;
    	if(context.containsBean(beanName)){
    		logger.debug(LogCategory.SYSTEM,
    				"获得Spring中应用ID为：{}，MQ队列序号为：{}的队列监听", new Object[]{appId, index});
    		return (MqListenerContainer)context.getBean(beanName);
    	}else {
    		logger.debug(LogCategory.SYSTEM,
    				"当前Spring中不存在应用ID为：{}，MQ队列序号为：{}的队列监听", new Object[]{appId, index});
    		return null;
    	}
    }
    
    /**
     * 
     * <p>通过应用ID及MQ队列序号，删除MQ队列监听</p> 
     * @author ztjie
     * @date 2015-11-19 下午3:08:06
     * @param appId 应用ID
     * @param index 应用配置的MQ队列序号
     * @return
     * @see
     */
    public void remove(String appId, Integer index){
    	String key = appId+"_"+index;
    	String beanName=MQManagerContants.CONTAINER+"_"+key;
    	if(context.containsBean(beanName)){
    		logger.debug(LogCategory.SYSTEM,
    				"当前Spring中删除应用ID为：{}，MQ队列序号为：{}的队列监听", new Object[]{appId, index});
    		MqListenerContainer bean = (MqListenerContainer)context.getBean(beanName);
    		bean.destroy();
    	}else {
    		logger.debug(LogCategory.SYSTEM,
    				"当前Spring中不存在应用ID为：{}，MQ队列序号为：{}的队列监听", new Object[]{appId, index});
		}
    }
    
    /**
     * 
     * <p>增加应用ID及MQ队列序号对应的MQ队列监听</p> 
     * @author ztjie
     * @date 2015-11-19 下午3:08:06
     * @param appId 	应用ID
     * @param index 	应用配置的MQ队列序号
     * @param template 	队列处理器
     * @return
     * @see
     */
    public MqListenerContainer add(String appId, Integer index, MqListenerContainer template){
    	String key = appId+"_"+index;
    	String beanName=MQManagerContants.CONTAINER+"_"+key;
    	template.setName(beanName);
		if(context.containsBean(beanName)){
			logger.debug(LogCategory.SYSTEM,
					"当前Spring中已存在应用ID为：{}，MQ队列序号为：{}的队列监听", new Object[]{appId, index});
			return null;
		}
		logger.debug(LogCategory.SYSTEM,
				"当前Spring中增加应用ID为：{}，MQ队列序号为：{}的队列监听", new Object[]{appId, index});
		beanDefinitionReader.loadBeanDefinitions(new MqContainerResource(template));
		return (MqListenerContainer)context.getBean(beanName);
    }
	@Override
	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		context = (ConfigurableApplicationContext)applicationContext;
		init();
	}
}
